package com.aube.views;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.aube.R;
import com.aube.activity.WapActivity;
import com.huyn.baseframework.ImageLoader;
import com.huyn.baseframework.dynamicload.video.model.GoodsItem;
import com.huyn.baseframework.utils.StringUtils;
import com.huyn.baseframework.utils.SysUtil;
import com.huyn.baseframework.utils.Utils;

import java.util.Iterator;
import java.util.Map;

/**
 * Created by huyaonan on 16/4/19.
 */
public class FavorCart extends LinearLayout {
    private Context mContext;

    private View mRoot;
    private TextView mTitle;
    private ScrollView mScroll;
    private LinearLayout mContent;

    private int MAX_HEIGHT = 0;

    public FavorCart(Context context) {
        this(context, null);
    }
    public FavorCart(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public FavorCart(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }
    private void init() {
        mRoot = LayoutInflater.from(mContext).inflate(R.layout.layout_favorcart, this);

        mTitle = (TextView) mRoot.findViewById(R.id.cart_title);
        mScroll = (ScrollView) mRoot.findViewById(R.id.cart_scroll);
        mContent = (LinearLayout) mRoot.findViewById(R.id.cart_list);

        //最高高度为屏幕的4/5
        MAX_HEIGHT = SysUtil.getAbsoluteScreenHeight(mContext) * 4/5;

        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                if(getHeight() > MAX_HEIGHT) {
                    ViewGroup.LayoutParams params = getLayoutParams();
                    params.height = MAX_HEIGHT;
                    setLayoutParams(params);
                }
            }
        });

        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setVisibility(View.GONE);
            }
        });
    }

    public boolean fill(Iterator<Map.Entry<String, GoodsItem>> iterator) {
        if(iterator == null)
            return false;

        mContent.removeAllViews();

        int index = 0;
        while (iterator.hasNext()) {
            final GoodsItem data = iterator.next().getValue();

            View view = LayoutInflater.from(mContext).inflate(R.layout.item_cart, null);

            ImageView mLogo = (ImageView) view.findViewById(R.id.cart_logo);
            TextView mDes = (TextView) view.findViewById(R.id.cart_des);
            View mDivider = view.findViewById(R.id.cart_divider);
            TextView mOperator = (TextView) view.findViewById(R.id.cart_operator);

            mContent.addView(view);

            view.setTag(data._id);

            if (index == 0)
                mDivider.setVisibility(View.GONE);
            else
                mDivider.setVisibility(View.VISIBLE);

            ImageLoader.getInstance(getContext()).fillImage(mLogo, data.getPic(), 200, 200);
            mDes.setText(data.tlTitle);

            mOperator.setText(data.goodstypedesc);

            mOperator.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    //do something
                    if(StringUtils.isNotBlank(data.goodsURL)) {
                        Intent intent = new Intent(mContext, WapActivity.class);
                        intent.putExtra(WapActivity.WEB_URL, data.goodsURL);
                        intent.putExtra(WapActivity.WEB_POST, true);
                        mContext.startActivity(intent);
                    } else {
                        Utils.showToast(mContext, "链接无效");
                    }
                }
            });

            index++;
        }

        if(index > 0) {
            mTitle.setText("您的欲望清单 (" + index + ")");
            return true;
        }
        return false;
    }

}
